package dao;

import entity.ProductDetails;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Created by jifeng on 2016/12/20 0020.
 */
public class ProductDetailsDao {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    public  void addProductDetails(ProductDetails productDetails){
        getCurrentSession().save(productDetails);
    }

    public ProductDetails findProductDetailsById(int id){
        String sql="SELECT * FROM ProductDetails ps WHERE ps.productId="+id+"";
        ProductDetails productDetails=(ProductDetails) getCurrentSession().createSQLQuery(sql).addEntity(ProductDetails.class).list().get(0);
        return productDetails;
    }

    public void updateProductDetails(ProductDetails productDetails){
        String sql="UPDATE productdetails pd SET pd.age='"+productDetails.getAge()+"' ,pd.brand='"+productDetails.getBrand()+"' ,pd.collarType='"+productDetails.getCollarType()+"' ,pd.color ='"+productDetails.getColor()+"' ,pd.elements='"+productDetails.getElements()+"' ,pd.fabric='"+productDetails.getFabric()+"' ,pd.object='"+productDetails.getObject()+"' ,pd.pendulumType='"+productDetails.getPendulumType()+"' ,pd.productCode='"+productDetails.getProductCode()+"' ,pd.scene='"+productDetails.getScene()+"' ,pd.size='"+productDetails.getSize()+"' ,pd.sleeve='"+productDetails.getSleeve()+"' ,pd.style='"+productDetails.getStyle()+"' ,pd.technology='"+productDetails.getTechnology()+"' ,pd.waistType='"+productDetails.getWaistType()+"' ,pd.year='"+productDetails.getYear()+"' WHERE pd.id="+productDetails.getId()+"";
        getCurrentSession().createSQLQuery(sql).executeUpdate();
    }

}
